package com.qbb.client.bean.active;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.qbb.client.aop.ConnsTypeUtil;
import com.qbb.client.aop.enums.QbbConnsTypeEnum;
import com.qbb.client.bean.result.SocketResultBean;
import com.qbb.client.bean.result.SocketResultListMapBean;
import com.qbb.client.bean.result.SocketResultMapBean;
import com.qbb.client.exception.HandlerException;
import com.qbb.client.exception.NettyException;
import com.qbb.client.security.SecurityUtil;
import com.qbb.client.util.JsonUtil;

/**
 * 类说明
 * 
 * @author chenhuaiao
 * @version V1.0 创建时间：Mar 12, 2015 5:16:36 PM
 */
public class SocketBean {
	private String serverName;// 模块

	/**
	 * 根据配置文件参数获得返回的参数对象
	 * 
	 * @param serverName
	 * @param obj
	 * @return
	 */
	public static SocketBean getBean(String serverName) {
		SocketBean socketBean = new SocketBean();
		socketBean.setServerName(serverName);
		return socketBean;
	}

	/**
	 * 根据配置文件参数获得返回的参数对象
	 * 
	 * @param serverName
	 * @param obj
	 * @return
	 */
	public static SocketBean getBean(String serverName, Object obj) {
		SocketBean socketBean = null;

		String classPath = obj.getClass().getName();

		QbbConnsTypeEnum myEnum = ConnsTypeUtil.getType(classPath);

		if (myEnum == QbbConnsTypeEnum.List) {
			socketBean = new SocketListMapBean();
		} else if (myEnum == QbbConnsTypeEnum.Map) {
			socketBean = new SocketMapBean();
		} else {
			boolean flag = false;
			try {
				JsonUtil.toHashMap(obj);
				socketBean = new SocketMapBean();
				flag = true;
			} catch (Exception e) {
				HandlerException.error(e);
				flag = false;
				socketBean = null;
			}
			try {
				if (!flag) {
					JsonUtil.toList(obj);
					socketBean = new SocketListMapBean();
					flag = true;
				}
			} catch (Exception e) {
				HandlerException.error(e);
				socketBean = null;
				flag = false;
			}
			if (!flag) {
				throw new NettyException("客户端传递给服务端接口的参数只能是List或者是Map集合!");
			}
		}

		socketBean.setServerName(serverName);
		socketBean.setResult(obj);
		return socketBean;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getUuid() {
		UUID uuid = UUID.randomUUID();
		String uuidStr = uuid.toString().replaceAll("-", "");
		return uuidStr;
	}

	@Override
	public String toString() {
		Class clas = this.getClass();
		String simpleName = clas.getSimpleName();
		String jsonStr = null;
		try {
			if ("SocketListMapBean".equals(simpleName)) {
				Method method = clas.getDeclaredMethod("getListMap");
				List<Map<String, Object>> listMaps = (List<Map<String, Object>>) method
						.invoke(this);
				jsonStr = JsonUtil.toJSONString(listMaps);
			} else if ("SocketMapBean".equals(simpleName)) {
				Method method = clas.getDeclaredMethod("getMap");
				Map<String, Object> map = (Map<String, Object>) method
						.invoke(this);
				jsonStr = JsonUtil.toJSONString(map);
			}
			return this.serverName + "|" + simpleName + "|" + jsonStr;
		} catch (Exception e) {
			HandlerException.error(e);
		}
		return null;
	}

	public void setResult(Object result) {
		Class clas = this.getClass();
		String simpleName = clas.getSimpleName();
		try {
			if ("SocketListMapBean".equals(simpleName)) {
				List<Map<String, Object>> list = (List<Map<String, Object>>) result;

				Method method = clas
						.getDeclaredMethod("setListMap", List.class);
				method.invoke(this, list);
			} else if ("SocketMapBean".equals(simpleName)) {
				Map<String, Object> map = (Map<String, Object>) result;
				Method method = clas.getDeclaredMethod("setMap", Map.class);
				method.invoke(this, map);
			}
		} catch (Exception e) {
			HandlerException.error(e);
		}
	}

	public static SocketResultBean getResultBean(Object msg) {
		SocketResultBean resultBean = null;
		String uuidStr = null;
		try {
			String msgStr = msg.toString();
			String descurityStr = SecurityUtil.getSecurityStr(msgStr,
					SecurityUtil.kek, 1);

			int descurityLen = descurityStr.indexOf("|");
			if (descurityLen == -1) {
				resultBean = new SocketResultBean();
				resultBean.setUuidString(uuidStr);
				return resultBean;
			}

			String lengStr = descurityStr.substring(0, descurityLen);
			int length = Integer.parseInt(lengStr);

			String all_esc_str = descurityStr.substring(0, length);

			String all_read_Str = all_esc_str.substring(descurityLen + 1);

			int uuidIndex = all_read_Str.indexOf("|");
			uuidStr = all_read_Str.substring(0, uuidIndex);

			String next_four_Str = all_read_Str.substring(uuidIndex + 1);
			int resultTypeIndex = next_four_Str.indexOf("|");
			String resultTypeStr = next_four_Str.substring(0, resultTypeIndex);

			String jsonStr = next_four_Str.substring(resultTypeIndex + 1);

			if ("SocketResultListMapBean".equals(resultTypeStr)) {
				List<Map<String, Object>> listMap = JsonUtil.toList(jsonStr);
				resultBean = new SocketResultListMapBean();
				Method method = resultBean.getClass().getDeclaredMethod(
						"setListMap", List.class);
				method.invoke(resultBean, listMap);
			} else if ("SocketResultMapBean".equals(resultTypeStr)) {
				Map<String, Object> map = JsonUtil.toHashMap(jsonStr);
				resultBean = new SocketResultMapBean();
				Method method = resultBean.getClass().getDeclaredMethod(
						"setMap", Map.class);
				method.invoke(resultBean, map);
			} else {
				resultBean = new SocketResultBean();
			}
			resultBean.setUuidString(uuidStr);

		} catch (Exception e) {
			HandlerException.error(e);
		} finally {
			if (resultBean == null) {
				resultBean = new SocketResultBean();
				return resultBean;
			}
			if (resultBean != null) {
				resultBean.setUuidString(uuidStr);
			}
		}
		return resultBean;
	}
}
